package org.gnyxy.controller.background;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.gnyxy.dto.MaterialDTO;
import org.gnyxy.dto.MaterialPageQueryDTO;
import org.gnyxy.entity.Material;
import org.gnyxy.result.PageResult;
import org.gnyxy.result.Result;
import org.gnyxy.service.MaterialService;
import org.gnyxy.vo.MaterialVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("AdminMaterialController")
@RequestMapping("/background/material")
@Slf4j
@Api(tags = "商家后台-菜品原材料")
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    /**
     * 新增菜品材料
     * @param materialDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增菜品原材料")
    @CacheEvict(cacheNames = "materialsCache",allEntries = true)
    public Result add(@RequestBody MaterialDTO materialDTO){
        log.info("添加菜品原材料: {}",materialDTO);
        materialService.add(materialDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param materialPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(MaterialPageQueryDTO materialPageQueryDTO){
        log.info("分页查询: {}",materialPageQueryDTO);
        PageResult pageResult =  materialService.page(materialPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 修改菜品材料
     * @param materialDTO
     * @return
     */
    @PostMapping("update")
    @ApiOperation("修改菜品原材料")
    @CacheEvict(cacheNames = "materialsCache",allEntries = true)
    public Result update(@RequestBody MaterialDTO materialDTO){
        log.info("修改菜品材料: {}",materialDTO);
        materialService.update(materialDTO);
        return Result.success();
    }

    /**
     * 根据id查询菜品原材料
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品原材料")
    public Result<MaterialVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品原材料: {}",id);
        MaterialVO materialVO =  materialService.getById(id);
        return Result.success(materialVO);
    }

    /**
     * 根据id删除菜品原材料
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    @ApiOperation("根据id删除菜品原材料")
    @CacheEvict(cacheNames = "materialsCache",allEntries = true)
    public Result deleteById(@PathVariable Long id){
        log.info("删除的id为: {}",id);
        materialService.deletebyId(id);
        return Result.success();
    }

    /**
     * 设置状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("设置状态(0禁用 1启用)")
    @CacheEvict(cacheNames = "materialsCache",allEntries = true)
    public Result setStatus(@PathVariable Integer status,Long id){
        log.info("设置状态为: {}", status == 0 ? "禁用":"启用");
        materialService.setStatus(status,id);
        return Result.success();
    }

    /**
     * 查看未被禁用的菜品材料
     * @return
     */
    @GetMapping("/getTrueStatus")
    @ApiOperation("查看未被禁用的菜品材料")
    public Result<List<MaterialVO>> getTrueStatus(String name){
        log.info("查看未被禁用的菜品材料: {}",name);
        List<MaterialVO> materialVOS = materialService.getTrueStatus(name);
        return Result.success(materialVOS);
    }
}
